package com.thoughtworks.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.thoughtworks.exception.GrammerException;
import com.thoughtworks.model.QuestionType;
import com.thoughtworks.model.Route;
import com.thoughtworks.model.Question;
import com.thoughtworks.model.CompareType;
import com.thoughtworks.model.Constants;

public class ParseQuestionUtil {
	
	/**
	 * @param question_file : question file that describe the questions 
	 * @return              : all Question object
	 * @throws IOException
	 * @throws GrammerException
	 */
	public static List<Question> geQuestions(String question_file) throws IOException, GrammerException {
		String content = ReadFileUtil.getFileContent(question_file);
		String[] lines = content.split("\n");
		List<Question> commandList = new ArrayList<Question>();
		for(String line : lines) {
			line = line.trim();
			if("".equals(line) || line.startsWith(Constants.COMMENT_SYMBOL)){
				continue;
			}
			commandList.add(new Question(getRoute(line), geQuestionType(line), geCompareType(line), getCompareNum(line)));
		}
		return commandList;
	}
	

	private static Route getRoute(String line) {
		String routeString;
		if(line.contains("{")) {
			routeString = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
		} else {
			routeString = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
		}
		return new Route(routeString);
	}
	
	private static QuestionType geQuestionType(String line) throws GrammerException {
		Pattern pattern1 = Pattern.compile(Constants.DISTANCE_OF_FIX_TRIP);
		
		Pattern pattern2 = Pattern.compile(Constants.SHORTEST_DISTANCE_OF_TRIP);
		
		Pattern pattern3 = Pattern.compile(Constants.SHORTEST_STOP_COUNT_OF_TRIP);
		
		Pattern pattern4 = Pattern.compile(Constants.COUNT_OF_TRIP_AS_STOP_COUNT_CONDITION);
		
		Pattern pattern5 = Pattern.compile(Constants.COUNT_OF_TRIP_AS_DISTANCE_CONDITION);
		
		if(pattern1.matcher(line).matches()) {
			return QuestionType.DISTANCE_OF_FIX_TRIP;
		} else if(pattern2.matcher(line).matches()) {
			return QuestionType.SHORTEST_DISTANCE_OF_TRIP;
		} else if(pattern3.matcher(line).matches()) {
			return QuestionType.SHORTEST_STOP_COUNT_OF_TRIP;
		} else if(pattern4.matcher(line).matches()) {
			return QuestionType.COUNT_OF_TRIP_AS_STOP_COUNT_CONDITION;
		} else if(pattern5.matcher(line).matches()) {
			return QuestionType.COUNT_OF_TRIP_AS_DISTANCE_CONDITION;
		} else {
			throw new GrammerException();
		}
	}
	
	private static CompareType geCompareType(String line) {
		if(line.contains("<=")) {
			return CompareType.LESS_THAN_OR_EQUAL;
		}else if(line.contains("<")) {
			return CompareType.LESS_THAN;
		}else if(line.contains("=")) {
			return CompareType.EQUAL_TO;
		}else {
			return CompareType.NONE;
		}
	}
	
	private static int getCompareNum(String line){
		String numStr = line.replaceAll("[^0-9]", "");
		if("".equals(numStr.trim())) {
			return 0;
		}
		return Integer.parseInt(numStr);
	}
}
